package br.com.webprojeto.colecoes;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.webprojeto.basicas.*;
import br.com.webprojeto.interfaces.InterfaceColecaoJuridica;



public class ColecaoJuridica implements InterfaceColecaoJuridica {
	/* (non-Javadoc)
	 * @see br.com.webprojeto.colecoes.InterfaceColecaoJuridica#adicionarJuridica(br.com.webprojeto.basicas.Juridica)
	 */
	@Override
	public void adicionarJuridica(Juridica jud) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		sessao.saveOrUpdate(jud);
		sessao.getTransaction().commit(); 
	}
	 
	/* (non-Javadoc)
	 * @see br.com.webprojeto.colecoes.InterfaceColecaoJuridica#removerJuridica(long)
	 */
	@Override
	public void removerJuridica(long codJuridica) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Juridica jud = (Juridica) sessao.load(Juridica.class, codJuridica);
		sessao.delete(jud);
		sessao.getTransaction().commit();
	}
	 
	/* (non-Javadoc)
	 * @see br.com.webprojeto.colecoes.InterfaceColecaoJuridica#atualizarJuridica(br.com.webprojeto.basicas.Juridica)
	 */
	@Override
	public void atualizarJuridica(Juridica jud) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		sessao.update(jud);
		sessao.getTransaction().commit();
	}
	 
	/* (non-Javadoc)
	 * @see br.com.webprojeto.colecoes.InterfaceColecaoJuridica#listarJuridica()
	 */
	@Override
	public List<Juridica> listarJuridica() {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Query consulta = sessao.createQuery("from Juridica");
		return consulta.list();
	}
	 
	/* (non-Javadoc)
	 * @see br.com.webprojeto.colecoes.InterfaceColecaoJuridica#procurarJuridica(long)
	 */
	@Override
	public Juridica procurarJuridica(long codJuridica) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Juridica jud = (Juridica) sessao.load(Juridica.class, codJuridica);
		sessao.getTransaction().commit();
		
		return jud;
	}

	/* (non-Javadoc)
	 * @see br.com.webprojeto.colecoes.InterfaceColecaoJuridica#localizarJuridicaCnpj(java.lang.String)
	 */
	@Override
	public Juridica localizarJuridicaCnpj(String cnpj) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Query consulta = sessao.createQuery("from Juridica where cnpj like ?");
		consulta.setParameter(0, cnpj);
		Juridica jud = (Juridica) consulta.uniqueResult();
		sessao.getTransaction().commit();
		return jud;
	}

}
